/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.audit.server  
 */
package com.apache.auditRule.server;

import com.apache.api.vo.ParamsVo;
import com.apache.auditRule.entity.Audit;
import com.apache.auditRule.entity.AuditRule;
import com.apache.auditRule.manager.AuditManager;
import com.apache.auditRule.manager.AuditRuleManager;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SpringContextLoader;
import com.apache.member.common.ConstantData;
import com.apache.tools.DateUtils;

public class AuditSaveHelper {
	private static AuditSaveHelper helper;
	private AuditManager auditManager;
	private AuditRuleManager auditRuleManager;

	public static AuditSaveHelper newIntance() {
		if (null == helper) {
			helper = new AuditSaveHelper();
		}
		return (helper);
	}

	public void auditSave(ParamsVo<Object> paramsVo) {

		AuditManager auditManager = getAuditManager();
		AuditRule art = (AuditRule) paramsVo.getObj();
		if (Validator.isEmpty(art)) {
			AuditRuleManager auditRuleManager = getAuditRuleManager();
			ParamsVo<AuditRule> auditRuleVo = new ParamsVo<AuditRule>();
			AuditRule ar = new AuditRule();
			ar.setTableName(String.valueOf(paramsVo.getParams("tableName")));
			ar.setAuditType(String.valueOf(paramsVo.getParams("auditType")));
			ar.setIfStart("T");
			auditRuleVo.setObj(ar);
			art = auditRuleManager.getByParams(auditRuleVo);//查询到的规则
		}

		if (!Validator.isEmpty(art)) {
			ParamsVo<Audit> auditVo = new ParamsVo<Audit>();
			Audit audit = new Audit();
			audit.setCheckUser(art.getCheckUser());
			audit.setPid(String.valueOf(paramsVo.getParams("pid")));//外键
			audit.setNextUser(art.getNext());
			audit.setTableName(art.getTableName());
			audit.setColName(String.valueOf(paramsVo.getParams("colName")));
			audit.setAuditType(String.valueOf(paramsVo.getParams("auditType")));
			audit.setAuditsStatus(ConstantData.AuditStatus.AUDITING);//新插入的数据为待审核状态
			audit.setNum(art.getNum());
			audit.setCreateTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
			audit.setCreateUserEname(String.valueOf(paramsVo.getParams("userEname")));
			audit.setUpdateTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
			audit.setUpdateUserEname(String.valueOf(paramsVo.getParams("userEname")));
			audit.setTaskName(String.valueOf(paramsVo.getParams("colName")));
			audit.setProId(String.valueOf(paramsVo.getParams("proId")));
			auditVo.setObj(audit);
			auditManager.saveInfo(auditVo);
		}

	}

	private AuditManager getAuditManager() {
		if (null == auditManager) {
			auditManager = (AuditManager) SpringContextLoader.getBean("auditManager");
		}
		return auditManager;
	}

	private AuditRuleManager getAuditRuleManager() {
		if (null == auditRuleManager) {
			auditRuleManager = (AuditRuleManager) SpringContextLoader.getBean("auditRuleManager");
		}
		return auditRuleManager;
	}
}
